<?php
namespace app\common\controller;
use think\Controller;

/*心率训练 公用class*/
class AdminBase extends  Controller
{
	  public function _initialize()
    {
    	if(empty(sp_get_current_admin())){
    		if(!empty(input("to_login"))){
    			  $this->redirect("/Admin/Main/login?login_type=".$_COOKIE['l_t']."&shop_id=".$_COOKIE['s_d']."&soft_version=".$_COOKIE['s_v']);
    		}else{
    				$this->ajax_error("身份已经过期，请点击右上角退出按钮，重新登录！");	
    		}
    	}
    }
	  
	  /**
	   * ajax 成功
	   * @param $msg 成功提示
	   * @param $data 数据
	   * */
	  protected function ajax_success($msg="",$data=''){
	  		header('Content-Type:application/json; charset=utf-8');
        exit(json_encode(array("flag"=>TRUE,"msg"=>$msg,"data"=>$data),JSON_UNESCAPED_UNICODE));	 
	  }
	  /**
	   * ajax 失败
	   * @param $msg 失败提示
	   * @param $data 数据
	   * */
	  protected function ajax_error($msg="",$data=''){
	  	  header("Content-Type:application/json;charset=utf-8");
		  	exit(json_encode(array("flag"=>FALSE,"msg"=>$msg,"data"=>$data),JSON_UNESCAPED_UNICODE));
	  }
	  /*ajax*/
	  protected function ajax($data=''){
	  	  header("Content-Type:application/json;charset=utf-8");
		  	exit(json_encode($data,JSON_UNESCAPED_UNICODE));
	  }
		/*获取查询页数*/
		protected function get_limit(){
			return input("startIndex",0).",".input("pageSize",20);
		}
		/**
		 * 获取排序信息
		 * $table_name 表名称
		 * */
		protected function get_order($table_name=''){
			if(empty($table_name)||strpos(input("orderColumn"), ".")){
					return input("orderColumn","id")." ".input("orderDir","desc");	
			}else{
					return $table_name.".".input("orderColumn","id")." ".input("orderDir","desc");
			}
		}
		
			protected function to_hex_revers($value){
		  if(strlen($value)==10){//10位10进制
		      $dec=str_pad(dechex(doubleval($value)), 8,"0",STR_PAD_LEFT);
		      $bit1=substr($dec, 0,2);
			  	$bit2=substr($dec, 2,2);
					$bit3=substr($dec, 4,2);
					$bit4=substr($dec, 6,2);
					return  strtoupper($bit4.$bit3.$bit2.$bit1);
		  }else{
		  	$this->ajax_error($data="",$code="0000",$msg="卡号不是10位的10进制数");
		  }
	}
			
			protected function to_dec_revers($value){
		  	  $bit1=substr($value, 0,2);
			  	$bit2=substr($value, 2,2);
					$bit3=substr($value, 4,2);
					$bit4=substr($value, 6,2);
					$dec=hexdec($bit4.$bit3.$bit2.$bit1);		
				return	str_pad($dec, 10,'0',STR_PAD_LEFT);		
	 }

    /**
     * 员工业绩记录
     * @param $employee_id
     * @param $money
     * @param int $shop_id
     */
    protected function employeeAchievement($money, $employee_id, $shop_id = 0)
    {
        if (empty($employee_id)) {
            return false;
        }
        if (empty($shop_id)) {
            $shop_id = db("gym_employee")->where(array("id" => $employee_id))->value("shop_id");
        }
        $year_month = date("Ym", time());

        //查找是否存在该员工本月业绩记录
        $achievement = db("gym_employee_achievement")->where(array("employee_id" => $employee_id, "year_month" => $year_month))->find();
        if (empty($achievement)) {
            db("gym_employee_achievement")->insert(array("shop_id" => $shop_id, "employee_id" => $employee_id, "achievement" => $money, "year_month" => $year_month));
        } else {
            db("gym_employee_achievement")->where(array("id" => $achievement["id"]))->setInc('achievement', $money);
        }
        return true;
    }

    /**
     * ajax模板
     */
    protected function ajax_tpl($msg){
        $this->assign("msg", $msg);
        echo $this->fetch(APP_PATH.request()->module(). "/view/public/ajax_tpl.html");
        exit();
    }
}
